var http = require('http');
var fs = require("fs");

//文件
function serverStaticFile(res,path,contentType,responseCode){
	if(!responseCode){
		responseCode = 200;
	}
	fs.readFile(__dirname+path,function(err,data){
		if(err){
			res.writeHead(500,{'Content-Type':'text/plain'});
			res.end("500 - Error");
		}else{
			res.writeHead(responseCode,{"Content-Type":contentType});
			res.end(data);
		}
	});
}

http.createServer( function(req, res){
	//去掉query_string 查询字符串
	var path = req.url.replace(/\/?(?:\?.*)$/,'').toLowerCase();

	//解析uri
	switch(path){
		case '': 
			res. writeHead( 200, { 'Content-Type': 'text/plain' }); 
			res. end('Homepage'); 
			break; 
		case '/about': 
			res. writeHead( 200, { 'Content-Type': 'text/plain' }); 
			res. end('About'); 
			break; 
		case '/home':
			serverStaticFile(res,'/home.html','text/html');
			break;
		default:
			res. writeHead( 404, { 'Content-Type': 'text/plain' }); 
			res. end('Not Found'); 
			break;
	}
}).listen(3000);

console.log("start on 3000");